﻿using Avalonia.Controls;
using Avalonia.Controls.Templates;
using Avalonia.Extensions.Controls;
using Bilibili.Common.Log;
using Bilibili.Model.Dynamic;
using PCLUntils.Json;
using PCLUntils.Objects;
using System;
using System.Linq;

namespace Avalonia.BiliBili.Libs.Models
{
    public sealed class DynamicItemCard : DynamicCards
    {
        public DynamicItemCard(DynamicCards dynamicCards)
        {
            Desc = dynamicCards.Desc;
            Type = dynamicCards.Type;
            Rid = dynamicCards.Rid;
            User_Profile = dynamicCards.User_Profile;
            Dynamic_Id = dynamicCards.Dynamic_Id;
            Repost = dynamicCards.Repost;
            Like = dynamicCards.Like;
            Is_Liked = dynamicCards.Is_Liked;
            Comment = dynamicCards.Comment;
            Orig_Dy_Id = dynamicCards.Orig_Dy_Id;
            Orig_Type = dynamicCards.Orig_Type;
            Card = dynamicCards.Card;
            Video = dynamicCards.Video;
            Bangumi = dynamicCards.Bangumi;
            Music = dynamicCards.Music;
            Article = dynamicCards.Article;
            Feed1 = dynamicCards.Feed1;
            Web = dynamicCards.Web;
            Feed = dynamicCards.Feed;
            MiniVideo = dynamicCards.MiniVideo;
            Display = dynamicCards.Display;
            Reply = dynamicCards.Reply;
            TimeStamp = dynamicCards.TimeStamp;
            Extension = dynamicCards.Extension;
            Vote_Cfg = dynamicCards.Vote_Cfg;
            InitControl();
        }
        private Control controlContent;
        public Control ControlContent => controlContent;
        private void InitControl()
        {
            try
            {
                switch (Desc.Type)
                {
                    case 1:
                        {
                            Feed = Card.ParseObject<DynamicFeed>();
                            Grid grid = new Grid();
                            grid.RowDefinitions.Add(new RowDefinition(GridLength.Auto));
                            grid.RowDefinitions.Add(new RowDefinition(GridLength.Star));
                            grid.Children.Add(new TextBlock { Text = Feed.item.content });
                            HandleFeed(0);
                            Grid content = new Grid();
                            Grid.SetRow(content, 1);
                            switch (Feed.item.orig_type)
                            {
                                case 2:
                                    {
                                        content.RowDefinitions.Add(new RowDefinition(GridLength.Auto));
                                        content.RowDefinitions.Add(new RowDefinition(GridLength.Star));
                                        content.Children.Add(new TextBlock { Text = Feed.photo.item.description });
                                        var pics = Feed.photo.item.pictures.Take(9);
                                        if (pics != null && pics.Count() > 0)
                                        {
                                            GridView gridView = new GridView { ColumnNum = 3 };
                                            gridView.ItemsSource = pics;
                                            gridView.ItemTemplate = new FuncDataTemplate<Pictures>((x, _) =>
                                            {
                                                return new ImageBox { Source = new Uri(x.img_src) };
                                            });
                                            Grid.SetRow(gridView, 1);
                                            content.Children.Add(gridView);
                                        }
                                        break;
                                    }
                                case 4:
                                    {
                                        if (Feed.origin_extension != null && Feed.origin_extension.vote_cfg != null)
                                        {
                                            StackPanel stackPanel = new StackPanel();
                                            stackPanel.Orientation = Layout.Orientation.Vertical;
                                            stackPanel.VerticalAlignment = Layout.VerticalAlignment.Center;
                                            stackPanel.Children.Add(new TextBlock { Text = Feed.origin_extension.vote_cfg.desc, Margin = new Thickness(36, 16, 0, 0) });
                                            stackPanel.Children.Add(new TextBlock { Text = Feed.origin_extension.vote_cfg.join_num.ToString(), Margin = new Thickness(36, 16, 0, 0) });
                                            Grid.SetColumn(stackPanel, 2);
                                            content.Children.Add(stackPanel);
                                        }
                                        break;
                                    }
                                case 8:
                                    {
                                        content.ColumnDefinitions.Add(new ColumnDefinition(240, GridUnitType.Pixel));
                                        content.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Star));
                                        content.Children.Add(new ImageBox { Source = new Uri(Feed.video.pic) });
                                        StackPanel stackPanel = new StackPanel();
                                        stackPanel.Orientation = Layout.Orientation.Vertical;
                                        stackPanel.VerticalAlignment = Layout.VerticalAlignment.Center;
                                        stackPanel.Children.Add(new TextBlock { Text = Feed.video.title, Margin = new Thickness(36, 16, 0, 0) });
                                        stackPanel.Children.Add(new TextBlock { Text = $"{Feed.video.stat.View}播放  {Feed.video.stat.Danmaku}弹幕", Margin = new Thickness(36, 16, 0, 0) });
                                        stackPanel.Children.Add(new TextBlock { Text = $"时长 {Feed.video.Duration}", Margin = new Thickness(36, 16, 0, 0) });
                                        Grid.SetColumn(stackPanel, 1);
                                        content.Children.Add(stackPanel);
                                        break;
                                    }
                                case 16:
                                    {
                                        content.ColumnDefinitions.Add(new ColumnDefinition(240, GridUnitType.Pixel));
                                        content.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Star));
                                        content.Children.Add(new ImageBox { Source = new Uri(Feed.minivideo.item.cover._default) });
                                        StackPanel stackPanel = new StackPanel();
                                        stackPanel.Orientation = Layout.Orientation.Vertical;
                                        stackPanel.VerticalAlignment = Layout.VerticalAlignment.Center;
                                        stackPanel.Children.Add(new TextBlock { Text = "投稿了视频", Margin = new Thickness(36, 16, 0, 0) });
                                        stackPanel.Children.Add(new TextBlock { Text = Feed.minivideo.item.description, Margin = new Thickness(36, 16, 0, 0) });
                                        content.Children.Add(stackPanel);
                                        break;
                                    }
                                case 64:
                                    {
                                        content.ColumnDefinitions.Add(new ColumnDefinition(240, GridUnitType.Pixel));
                                        content.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Star));
                                        content.Children.Add(new ImageBox { Source = new Uri(Feed.article.Banner_Url) });
                                        StackPanel stackPanel = new StackPanel();
                                        stackPanel.Orientation = Layout.Orientation.Vertical;
                                        stackPanel.VerticalAlignment = Layout.VerticalAlignment.Center;
                                        stackPanel.Children.Add(new TextBlock { Text = $"文章    {Feed.article.Title}", Margin = new Thickness(36, 16, 0, 0) });
                                        stackPanel.Children.Add(new TextBlock { Text = Feed.article.Summary, Margin = new Thickness(36, 16, 0, 0) });
                                        stackPanel.Children.Add(new TextBlock { Text = $"{Feed.article.Stats.ViewStr} 阅读", Margin = new Thickness(36, 16, 0, 0) });
                                        content.Children.Add(stackPanel);
                                        break;
                                    }
                                case 256:
                                    {
                                        content.ColumnDefinitions.Add(new ColumnDefinition(240, GridUnitType.Pixel));
                                        content.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Star));
                                        content.Children.Add(new ImageBox { Source = new Uri(Feed.music.cover) });
                                        content.Children.Add(new TextBlock
                                        {
                                            Text = $"投稿了音频    {Feed.music.title}",
                                            Margin = new Thickness(36, 0, 0, 0),
                                            VerticalAlignment = Layout.VerticalAlignment.Center
                                        });
                                        break;
                                    }
                                case 2048:
                                    {
                                        content.ColumnDefinitions.Add(new ColumnDefinition(240, GridUnitType.Pixel));
                                        content.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Star));
                                        content.Children.Add(new ImageBox { Source = new Uri(Feed.web.sketch.cover_url) });
                                        StackPanel stackPanel = new StackPanel();
                                        stackPanel.Orientation = Layout.Orientation.Vertical;
                                        stackPanel.VerticalAlignment = Layout.VerticalAlignment.Center;
                                        stackPanel.Children.Add(new TextBlock { Text = Feed.web.sketch.title, Margin = new Thickness(36, 16, 0, 0) });
                                        stackPanel.Children.Add(new TextBlock { Text = Feed.web.sketch.desc_text, Margin = new Thickness(36, 16, 0, 0) });
                                        content.Children.Add(stackPanel);
                                        break;
                                    }
                                case 4200:
                                    {
                                        content.Children.Add(new TextBlock { Text = Feed.item.content });
                                        break;
                                    }
                                default:
                                    {
                                        content.Children.Add(new TextBlock { Text = "未适配的类型，请等待更新" });
                                        break;
                                    }
                            }
                            grid.Children.Add(content);
                            controlContent = grid;
                            break;
                        }
                    case 2:
                    case 4:
                        {
                            HandleFeed(1);
                            Grid grid = new Grid();
                            grid.RowDefinitions.Add(new RowDefinition(GridLength.Auto));
                            grid.RowDefinitions.Add(new RowDefinition(GridLength.Star));
                            grid.RowDefinitions.Add(new RowDefinition(GridLength.Auto));
                            grid.Children.Add(new TextBlock { Text = Feed1.item.description });
                            var pics = Feed1.item.showpictures;
                            if (pics != null && pics.Count > 0)
                            {
                                GridView gridView = new GridView { ColumnNum = 3 };
                                gridView.ItemsSource = pics;
                                gridView.ItemTemplate = new FuncDataTemplate<Pictures>((x, _) =>
                                {
                                    return new ImageBox { Source = new Uri(x.img_src) };
                                });
                                Grid.SetRow(gridView, 1);
                                grid.Children.Add(gridView);
                            }
                            if (Extension != null && Extension.Vote_Cfg != null)
                            {
                                StackPanel stackPanel = new StackPanel();
                                stackPanel.Orientation = Layout.Orientation.Vertical;
                                stackPanel.VerticalAlignment = Layout.VerticalAlignment.Center;
                                stackPanel.Children.Add(new TextBlock { Text = Extension.Vote_Cfg.desc, Margin = new Thickness(36, 16, 0, 0) });
                                stackPanel.Children.Add(new TextBlock { Text = Extension.Vote_Cfg.join_num.ToString(), Margin = new Thickness(36, 16, 0, 0) });
                                Grid.SetColumn(stackPanel, 2);
                                grid.Children.Add(stackPanel);
                            }
                            controlContent = grid;
                            break;
                        }
                    case 8:
                        {
                            Video = Card.ParseObject<DynamicVideo>();
                            Grid grid = new Grid();
                            grid.ColumnDefinitions.Add(new ColumnDefinition(240, GridUnitType.Pixel));
                            grid.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Star));
                            grid.Children.Add(new ImageBox { Source = new Uri(Video.pic) });
                            StackPanel stackPanel = new StackPanel();
                            Grid.SetColumn(stackPanel, 1);
                            stackPanel.Orientation = Layout.Orientation.Vertical;
                            stackPanel.VerticalAlignment = Layout.VerticalAlignment.Center;
                            stackPanel.Children.Add(new TextBlock { Text = Video.title, Margin = new Thickness(36, 16, 0, 0) });
                            stackPanel.Children.Add(new TextBlock { Text = $"{Video.stat.View}播放  {Video.stat.Danmaku}弹幕", Margin = new Thickness(36, 16, 0, 0) });
                            stackPanel.Children.Add(new TextBlock { Text = $"时长 {Video.Duration}", Margin = new Thickness(36, 16, 0, 0) });
                            Grid.SetColumn(stackPanel, 1);
                            grid.Children.Add(stackPanel);
                            controlContent = grid;
                            break;
                        }
                    case 16:
                        {
                            MiniVideo = Card.ParseObject<DynamicMiniVideo>();
                            Grid grid = new Grid();
                            grid.ColumnDefinitions.Add(new ColumnDefinition(240, GridUnitType.Pixel));
                            grid.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Star));
                            if (MiniVideo.item.cover._default.IsNotEmpty())
                                grid.Children.Add(new ImageBox { Source = new Uri(MiniVideo.item.cover._default) });
                            StackPanel stackPanel = new StackPanel();
                            Grid.SetColumn(stackPanel, 1);
                            stackPanel.Orientation = Layout.Orientation.Vertical;
                            stackPanel.VerticalAlignment = Layout.VerticalAlignment.Center;
                            stackPanel.Children.Add(new TextBlock { Text = "投稿了视频", Margin = new Thickness(36, 16, 0, 0) });
                            stackPanel.Children.Add(new TextBlock { Text = MiniVideo.item.description, Margin = new Thickness(36, 16, 0, 0) });
                            grid.Children.Add(stackPanel);
                            controlContent = grid;
                            break;
                        }
                    case 64:
                        {
                            Article = Card.ParseObject<DynamicArticle>();
                            Grid grid = new Grid();
                            grid.ColumnDefinitions.Add(new ColumnDefinition(240, GridUnitType.Pixel));
                            grid.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Star));
                            if (Article.Banner_Url.IsNotEmpty())
                                grid.Children.Add(new ImageBox { Source = new Uri(Article.Banner_Url) });
                            StackPanel stackPanel = new StackPanel();
                            Grid.SetColumn(stackPanel, 1);
                            stackPanel.Orientation = Layout.Orientation.Vertical;
                            stackPanel.VerticalAlignment = Layout.VerticalAlignment.Center;
                            stackPanel.Children.Add(new TextBlock { Text = $"文章    {Article.Title}", Margin = new Thickness(36, 16, 0, 0) });
                            stackPanel.Children.Add(new TextBlock { Text = Article.Summary, Margin = new Thickness(36, 16, 0, 0) });
                            stackPanel.Children.Add(new TextBlock { Text = $"{Article.Stats.ViewStr} 阅读", Margin = new Thickness(36, 16, 0, 0) });
                            grid.Children.Add(stackPanel);
                            controlContent = grid;
                            break;
                        }
                    case 256:
                        {
                            Music = Card.ParseObject<DynamicMusic>();
                            Grid grid = new Grid();
                            grid.ColumnDefinitions.Add(new ColumnDefinition(240, GridUnitType.Pixel));
                            grid.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Star));
                            if (Music.cover.IsNotEmpty())
                                grid.Children.Add(new ImageBox { Source = new Uri(Music.cover) });
                            grid.Children.Add(new TextBlock
                            {
                                Text = $"投稿了音频    {Music.title}",
                                Margin = new Thickness(36, 0, 0, 0),
                                VerticalAlignment = Layout.VerticalAlignment.Center
                            });
                            controlContent = grid;
                            break;
                        }
                    case 512:
                    case 10001:
                        {
                            Bangumi = Card.ParseObject<DynamicBangumi>();
                            Grid grid = new Grid();
                            grid.ColumnDefinitions.Add(new ColumnDefinition(240, GridUnitType.Pixel));
                            grid.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Star));
                            if (Bangumi.Cover.IsNotEmpty())
                                grid.Children.Add(new ImageBox { Source = new Uri(Bangumi.Cover) });
                            StackPanel stackPanel = new StackPanel();
                            Grid.SetColumn(stackPanel, 1);
                            stackPanel.Orientation = Layout.Orientation.Vertical;
                            stackPanel.VerticalAlignment = Layout.VerticalAlignment.Center;
                            stackPanel.Children.Add(new TextBlock { Text = $"{Bangumi.ApiSeasonInfo.Title}更新了", Margin = new Thickness(36, 16, 0, 0) });
                            stackPanel.Children.Add(new TextBlock { Text = Bangumi.New_Desc, Margin = new Thickness(36, 16, 0, 0) });
                            stackPanel.Children.Add(new TextBlock { Text = Mode, Margin = new Thickness(36, 16, 0, 0) });
                            grid.Children.Add(stackPanel);
                            controlContent = grid;
                            break;
                        }
                    case 2048:
                        {
                            Web = Card.ParseObject<DynamicWeb>();
                            Grid grid = new Grid();
                            grid.ColumnDefinitions.Add(new ColumnDefinition(240, GridUnitType.Pixel));
                            grid.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Star));
                            if (Web.sketch.cover_url.IsNotEmpty())
                                grid.Children.Add(new ImageBox { Source = new Uri(Web.sketch.cover_url) });
                            StackPanel stackPanel = new StackPanel();
                            Grid.SetColumn(stackPanel, 1);
                            stackPanel.Orientation = Layout.Orientation.Vertical;
                            stackPanel.VerticalAlignment = Layout.VerticalAlignment.Center;
                            stackPanel.Children.Add(new TextBlock { Text = Web.sketch.title, Margin = new Thickness(36, 16, 0, 0) });
                            stackPanel.Children.Add(new TextBlock { Text = Web.sketch.desc_text, Margin = new Thickness(36, 16, 0, 0) });
                            grid.Children.Add(stackPanel);
                            controlContent = grid;
                            break;
                        }
                    default:
                        {
                            controlContent = new TextBlock { Text = "未适配的类型，请等待更新" };
                            break;
                        }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("CreateXaml", ex);
            }
        }
    }
}